#include "Camera.h"



Camera::Camera(glm::vec3& pos)
{
	position  = pos;
	up        = glm::vec3(0.0f, 1.0f, 0.0f);
	direction = glm::vec3(0.0f, 0.0f, -1.0f);

	/*
	Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector
	pointing to the right (due to how Euler angles work) so we initially rotate a bit to
	the left.
	*/
	yaw   = -90.0f;
	pitch = 0.0f;
	zoom  = 45.0f;
}

Camera::~Camera()
{
}

glm::mat4 Camera::GetViewMatrix()
{
	return glm::lookAt(position, position + direction, up);
}

void Camera::ProcessKeyboard(Behavior behavior, double deltaTime)
{
	GLfloat cameraSpeed = 5.0f * deltaTime;
	switch (behavior)
	{
	case Camera::FORWARD:
		position += cameraSpeed * direction;
		break;
	case Camera::BACKWARD:
		position -= cameraSpeed * direction;
		break;
	case Camera::LEFT:
		position -= cameraSpeed * glm::normalize(glm::cross(direction, up));
		break;
	case Camera::RIGHT:
		position += cameraSpeed * glm::normalize(glm::cross(direction, up));
		break;
	default:
		break;
	}
}

void Camera::ProcessMouseMovement(double xoffset, double yoffset)
{
	GLfloat sensitivity = 0.05f;
	xoffset *= sensitivity;
	yoffset *= sensitivity;

	yaw += xoffset;
	pitch += yoffset;
	if (pitch > 89.0f) {
		pitch = 89.0f;
	}
	else if (pitch < -89.0f) {
		pitch = -89.0f;
	}

	glm::vec3 front;
	front.y = sin(glm::radians(pitch));
	front.x = cos(glm::radians(pitch))*cos(glm::radians(yaw));
	front.z = cos(glm::radians(pitch))*sin(glm::radians(yaw));
	direction = glm::normalize(front);
}

void Camera::ProcessMouseScroll(double yoffset)
{
	if (zoom >= 1.0f && zoom <= 45.0f) {
		zoom -= yoffset;
	}
	if (zoom <= 1.0f) {
		zoom = 1.0f;
	}
	if (zoom >= 45.0f) {
		zoom = 45.0f;
	}
}

